package org.demo.patten.observer;

import java.util.ArrayList;
import java.util.List;

/**
 * @author zhuj
 * @date 2025/3/29
 * @apiNote
 */
public class WeatherStation implements Subject {
    private List<Observer> observers = new ArrayList<>();
    private String weather;
    public void setWeather(String weather) {
        this.weather = weather;
        notifyObservers(); // 状态变更即通知
    }
    public void attach(Observer observer) {
        observers.add(observer);
    }
    public void detach(Observer observer) {
        observers.remove(observer);
    }
    public void notifyObservers() {
        for (Observer observer : observers) {
            observer.update(weather);
        }
    }
}
